package com.wunu.smartlock1.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.clj.fastble.data.BleDevice
import com.wunu.smartlock1.model.ScannedPeripheral
import com.wunu.smartlock1.R

/**
 * Created by peiwei on 2018/4/21.
 */
public class ScanListAdapter(val scannedList: List<BleDevice>, val listener: OnItemClickListener): RecyclerView.Adapter<ScanListAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ScanListAdapter.ViewHolder {
        val view = LayoutInflater.from(parent!!.context).inflate(R.layout.smartlock_peripheral_activity,parent, false)
        val scale = parent!!.context.resources.displayMetrics.density
        val height = 50 * scale
        view.layoutParams.height = height.toInt()
        return ViewHolder(view)
    }

    override fun getItemCount(): Int  = scannedList.count()

    override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
        val device = scannedList.get(position)
        val titleView = holder?.view?.findViewById<TextView>(R.id.title_textView)
        titleView?.text = "智能锁: " + device.name
        val subtitleView = holder?.view?.findViewById<TextView>(R.id.subtitle_textview)
        subtitleView?.text = "信号强度: " + device.rssi.toString()
        holder?.view?.setOnClickListener {
            listener.onItemClick(holder!!.view, position)
        }
    }

    class ViewHolder(val view: View): RecyclerView.ViewHolder(view) {
    }
    public interface OnItemClickListener {
        public fun onItemClick(view: View, position: Int)
    }

}